﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VehicleManagement.Models;
using VehicleManagement.Codes;

namespace VehicleManagement.ControllersServices
{
    public class AccidentService
    {
        public IList<Accident> GetAllAccidents()
        {
            IList<Accident> lstModel = SqlMapperManager.Instance.Get().QueryForList<Accident>("SELECT_ALL_ACCIDENT", null);
            return lstModel;
        }

        public IList<Accident> GetAllAccidentsEx()
        {
            IList<Accident> lstModel = SqlMapperManager.Instance.Get().QueryForList<Accident>("SELECT_ALL_ACCIDENT_FOR_DISPLAY", null);
            return lstModel;
        }

        public object InsertAccident(Accident accident)
        {
            return SqlMapperManager.Instance.Get().Insert("INSERT_ACCIDENT", accident);
        }

        public object InsertAccidentEx(Accident accident)
        {
            convertAccidentForDatabase(ref accident);
            return InsertAccident(accident);
        }

        public int UpdateAccident(Accident accident)
        {
            return SqlMapperManager.Instance.Get().Update("UPDATE_ACCIDENT", accident);
        }

        public int UpdateAccidentEx(Accident accident)
        {
            convertAccidentForDatabase(ref accident);
            return UpdateAccident(accident);
        }

        public int DeleteAccident(int id)
        {
            return SqlMapperManager.Instance.Get().Delete("DELETE_ACCIDENT", id);
        }

        private void convertAccidentForDatabase(ref Accident accident)
        {
            CarInfoServices cis = new CarInfoServices();
            DriverServices ds = new DriverServices();
            accident.CarID = cis.GetCarIdByCarNum(accident.CarNumber);
            accident.DriverID = ds.GetDriverIdByDriverName(accident.DriverName);
        }

        public IList<Accident> SearchAccidents(string carNumber, string driverName, string accident)
        {
            IList<Accident> lstModel = SqlMapperManager.Instance.Get().QueryForList<Accident>("SEARCH_ACCIDENT", new
            {
                CarNumber = carNumber,
                DriverName = driverName,
                Accident = accident
            });
            return lstModel;
        }

        public IList<Accident> SearchAccidents(string carNumber, string driverName, string accident,int skip,int max)
        {
            IList<Accident> lstModel = SqlMapperManager.Instance.Get().QueryForList<Accident>("SEARCH_ACCIDENT", new
            {
                CarNumber = carNumber,
                DriverName = driverName,
                Accident = accident
            }, skip, max);
            return lstModel;
        }
    }
}